class  Calculator {
  // 字符串
  a = ''

  // 字符串
  b = ''

  // OptBtn
  opt = null 

  get display () {
    return this.b || this.a
  }

  btns = [
    new NumBtn('7', this), new NumBtn('8', this), new NumBtn('9', this), new AddOptBtn('+', this),
    new NumBtn('4', this), new NumBtn('5', this), new NumBtn('6', this), new SubOptBtn('-', this),
    new NumBtn('1', this), new NumBtn('2', this), new NumBtn('3', this), new MulOptBtn('*', this),
    new NumBtn('0', this), new ClearBtn('C', this), new EqOptBtn('=', this), new DivOptBtn('/', this),
  ]
}


class Btn {
  constructor (value, calculator) {
    this.value = value
    this.calculator = calculator
  }

  press () {
    // override
  }
}

class NumBtn extends Btn {

  press () {
    // TODO 输入校验
    if (!this.calculator.opt) {
      this.calculator.a = this.calculator.a + this.value
    } else {
      this.calculator.b = this.calculator.b + this.value
    }
  }
}

class OptBtn extends Btn {
  
  press () {
    if (!this.calculator.a) return
    if (!this.calculator.b) { // 只储存
      this.calculator.opt = this
    } else { // 进行计算
      const r = this.calculator.opt.calc(this.calculator.a, this.calculator.b)
      this.calculator.a = String(r)
      this.calculator.opt = this
      this.calculator.b = ''
    }
  }

  
  calc (a, b) {
    // override
  }
}

class AddOptBtn extends OptBtn {
  calc (a, b) {
    return String(Number(a) + Number(b))
  }
}

class SubOptBtn extends OptBtn {
  calc (a, b) {
    return String(Number(a) - Number(b))
  }
}

class MulOptBtn extends OptBtn {
  calc (a, b) {
    return String(Number(a) * Number(b))
  }
}


class DivOptBtn extends OptBtn {
  calc (a, b) {
    return String(Number(a) / Number(b))
  }
}

class EqOptBtn extends OptBtn {
  calc (a, b) {
    return b
  }
}

class ClearBtn extends Btn {
  press () {
    this.calculator.a = ''
    this.calculator.b = ''
    this.calculator.opt = null
  }
}